﻿#pragma once

#include "Logging.h"
#include <GetPot>
#include <memory>
#include <string>
#include <vector>

class CommandLine
{
public:
  CommandLine(int argc, char* argv[])
  {
    for (size_t i = 0; i < argc; i++)
    {
      _args.push_back(argv[i]);
    }
    _cl = std::make_shared<GetPot>(argc, argv);

    std::string ifile = _cl->follow("", "-i");
    if (this->Argc() == 1 || _cl->search(2, "-h", "--help") || ifile.empty())
    {
      this->PrintUsage();
      std::exit(0);
    }
  }

  ~CommandLine() {}

  void PrintUsage()
  {
    auto usage = fmt::format("Usage: {} -i <input.i>", _args[0]);
    std::cout << usage;
  }

  std::string Follow(const char* option) { return _cl->follow("", option); }
  GetPot& Get() { return *_cl; }
  int Argc() { return _args.size(); }

  template<typename T>
  T GetValue(const std::string& name)
  {
    try
    {
      return _cl->follow("", name.c_str());
    }
    catch (const std::exception& e)
    {
      Error(e.what());
    }
  }

private:
  std::shared_ptr<GetPot> _cl;
  std::vector<std::string> _args;
};
